<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\History;
use App\Models\Meeting;
use App\Services\Es;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class UserController extends Controller
{
    public function history(Request $request){
        $id = $request->get('id');
        $data = History::with('info')->where('user_id',$id)->paginate(5);
        return ['code'=>200,'msg'=>'success','data'=>$data];
    }
    public function search(Request $request){
        $name = $request->get('name');
        $data=(new Es())->search($name);
        return ['code'=>200,'msg'=>'success','data'=>$data];
    }

    /**
     * 用户进入会议
     * @param Request $request
     * @return array
     */
    public function intoMeeting(Request $request){
        $meeting_num = $request->get('num');
        $user_id=$request->get('id');
        $res = Meeting::where('meeting_num',$meeting_num)->first();
        $time = date('Y-m-d G:i:s');
        if (empty($res)){
            return ['code'=>202,'msg'=>'会议号不存在','data'=>''];
        }
        if ($time>$res['end_time']){
            return ['code'=>202,'msg'=>'会议已经结束','data'=>''];
        }else if($res['begin_time']>$time){
            return ['code'=>202,'msg'=>'会议还没开始','data'=>''];
        }
        if (empty(Cache::get("$meeting_num".'num'))){
            Cache::set("$meeting_num".'state',1);
        }
        return ['code'=>200,'msg'=>'success','data'=>''];
    }
    public function first(){
//        $time = date('Y-m-d G:i:s');
        $time2 = time();
        $data = Meeting::all()->toArray();
        dd($time2);
    }
}
